package org.example;

public class Test3 {
    //leetcode 700 二叉搜索树中的搜索
    public TreeNode searchBST(TreeNode root, int val) {
        if (root == null) return null;
        if (root.val == val) return root;
        else if (root.val > val) return searchBST(root.left,val);
        else return searchBST(root.right,val);
    }

    /**
     * 迭代
     * @param root
     * @param val
     * @return
     */
    public TreeNode searchBST1(TreeNode root, int val) {
        while (root != null && root.val != val) {
            if (root.val == val) return root;
            else if (root.val > val) root = root.left;
            else root = root.right;
        }
        return root;
    }
}
